package com.cdd.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdd.eduservice.entity.EduSubject;
import com.cdd.eduservice.service.IEduSubjectService;
import com.cdd.servicebase.handler.SmartException;
import com.cdd.eduservice.entity.excel.SubjectDate;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


//弹幕提示说添加@Component
public class SubjectExcelListener extends AnalysisEventListener<SubjectDate> {

    // SubjectExcelListener不能交给spring管理，需要自己new，不能注入其他对象
    // 不能实现数据库操作
    public IEduSubjectService iEduSubjectService;

    public SubjectExcelListener() {}

    public SubjectExcelListener(IEduSubjectService iEduSubjectService) {
        this.iEduSubjectService = iEduSubjectService;
    }

    // 一行一行读取excel内容,第一个值为一级分类，第二个值为二级分类
    @Override
    public void invoke(SubjectDate subjectDate, AnalysisContext analysisContext) {
        if(subjectDate == null){
            throw new SmartException(20001,"文件数据为空");
        }
        // 判断一级分类是否存在
        EduSubject existOneSubject = this.existOneSubject(iEduSubjectService, subjectDate.getOneSubjectName());
        if(existOneSubject == null) {
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectDate.getOneSubjectName());

            Date date = new Date();
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            existOneSubject.setGmtCreate(localDateTime);
            existOneSubject.setGmtModified(localDateTime);
            iEduSubjectService.save(existOneSubject);
        }

        // 获取一级分类的ID值
        String pid = existOneSubject.getId();
        EduSubject existTwoSubject = this.existTwoSubject(iEduSubjectService, subjectDate.getTwoSubjectName(), pid);
        if(existTwoSubject == null) {
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(subjectDate.getTwoSubjectName());

            Date date = new Date();
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            existTwoSubject.setGmtCreate(localDateTime);
            existTwoSubject.setGmtModified(localDateTime);
            iEduSubjectService.save(existTwoSubject);
        }
    }

    // 一级分类不能重复添加
    private EduSubject existOneSubject(IEduSubjectService iEduSubjectService, String name) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject one = iEduSubjectService.getOne(wrapper);
        return one;
    }

    // 二级分类不能重复添加
    private EduSubject existTwoSubject(IEduSubjectService iEduSubjectService, String name, String pid) {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject two = iEduSubjectService.getOne(wrapper);
        return two;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }



}
